package com.talent.jiwen_teacher;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.entity.LocalMedia;
import com.talent.jiwen_teacher.base.BaseActivity;
import com.talent.jiwen_teacher.ui.widgets.LoadDialog;
import com.talent.jiwen_teacher.util.ImageUtil;
import com.talent.jiwen_teacher.util.Validators;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by qyqx on 2018/8/11.
 * 上传身份证
 */

public class UpIDCardActivity extends BaseActivity {

    @BindView(R.id.img_id_1)
    ImageView img_id_1;
    @BindView(R.id.up_photo_1)
    Button up_photo_1;
    @BindView(R.id.img_id_2)
    ImageView img_id_2;
    @BindView(R.id.up_photo_2)
    Button up_photo_2;

    @BindView(R.id.img_li)
    ImageView img_li;

    @BindView(R.id.img_li_1)
    ImageView img_li_1;

    private String img1Url;
    private String img2Url;
    private int style;
    public static final String IMG_ID_1="IMG_ID_1";
    public static final String IMG_ID_2="IMG_ID_2";

    private List<LocalMedia> pathList = new ArrayList<>();//选择的照片路径
    private List<String> imgList = new ArrayList<>();//压缩后的图片显示路径

    @Override
    protected int provideContentViewId() {
        return R.layout.activirty_upid;
    }

    @Override
    public String titleName() {
        return "身份证";
    }

    @Override
    protected void initData() {

        mBtnLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setData();

            }
        });


        img1Url=getIntent().getStringExtra(IMG_ID_1);
        if(!Validators.isEmpty(img1Url)){
            img_li.setVisibility(View.GONE);
            ImageUtil.loadImageWithDefault(mContext, img1Url, R.mipmap.card_1, img_id_1);
        }

        img2Url=getIntent().getStringExtra(IMG_ID_2);
        if(!Validators.isEmpty(img2Url)){
            img_li_1.setVisibility(View.GONE);
            ImageUtil.loadImageWithDefault(mContext, img2Url, R.mipmap.card_2, img_id_2);
        }

    }

    private List<String> showList = new ArrayList<>();
    @OnClick({R.id.img_id_1,R.id.up_photo_1,R.id.img_id_2,R.id.up_photo_2})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_id_1:
                if(!Validators.isEmpty(img1Url)){
                    showList.clear();
                    showList.add(img1Url);
                    ImageUtil.picturePreviewWithUrl(UpIDCardActivity.this,0,showList);
                }
                break;
            case R.id.up_photo_1:
                style=1;
                imgPath=null;
                pathList.clear();
                ImageUtil.pickPhoto(UpIDCardActivity.this,1,pathList,true);
                break;

            case R.id.img_id_2:
                if(!Validators.isEmpty(img2Url)){
                    showList.clear();
                    showList.add(img2Url);
                    ImageUtil.picturePreviewWithUrl(UpIDCardActivity.this,0,showList);
                }
                break;
            case R.id.up_photo_2:
                style=2;
                imgPath=null;
                pathList.clear();
                ImageUtil.pickPhoto(UpIDCardActivity.this,1,pathList,true);
                break;
        }}

    @Override
    public void onBackPressed() {
        setData();
        super.onBackPressed();
    }

    private void setData(){

        if(!Validators.isEmpty(img1Url)&& !Validators.isEmpty(img2Url)){
            Intent intent=new Intent();
            intent.putExtra(IMG_ID_1,img1Url);
            intent.putExtra(IMG_ID_2,img2Url);
            setResult(1002,intent);
            finish();
        }
    }


    String imgPath;
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case PictureConfig.CHOOSE_REQUEST:
                    pathList.clear();
                    pathList.addAll(PictureSelector.obtainMultipleResult(data));

                    imgList.clear();
                    for (LocalMedia localMedia : pathList) {
                        imgList.add(localMedia.getCompressPath());
                    }
                    if(!Validators.isEmpty(imgList)&& imgList.size()>0){
                        imgPath=imgList.get(0);
                        if(style==1){
                            if(!Validators.isEmpty(imgPath)){
                                LoadDialog.show(UpIDCardActivity.this);
                                ImageUtil.upLoadSingleImg(imgPath, new ImageUtil.ImgUpLoadListener() {
                                    @Override
                                    public void OnSuccess(String successUrl) {
                                        img1Url=successUrl;
                                        img_li.setVisibility(View.GONE);
                                        showToast("上传成功");
                                        ImageUtil.loadImageWithDefault1(mContext,successUrl,R.mipmap.card_1,img_id_1);
                                        LoadDialog.dismiss(UpIDCardActivity.this);
                                        setData();
                                    }

                                    @Override
                                    public void OnError(String message) {
                                        showToast("上传失败，请重新上传");
                                        LoadDialog.dismiss(UpIDCardActivity.this);
                                    }

                                    @Override
                                    public void OnListUpSuccess(List<String> successFileList) {

                                    }
                                });
                            }else{
                                showToast("请先选择照片");
                            }

                        }else if(style==2){
                            if(!Validators.isEmpty(imgPath)){
                                LoadDialog.show(UpIDCardActivity.this);
                                ImageUtil.upLoadSingleImg(imgPath, new ImageUtil.ImgUpLoadListener() {
                                    @Override
                                    public void OnSuccess(String successUrl) {
                                        img2Url=successUrl;
                                        img_li_1.setVisibility(View.GONE);
                                        showToast("上传成功");
                                        ImageUtil.loadImageWithDefault1(mContext,img2Url,R.mipmap.card_2,img_id_2);
                                        LoadDialog.dismiss(UpIDCardActivity.this);
//                                        setData();
                                    }

                                    @Override
                                    public void OnError(String message) {
                                        showToast("上传失败，请重新上传");
                                        LoadDialog.dismiss(UpIDCardActivity.this);
                                    }

                                    @Override
                                    public void OnListUpSuccess(List<String> successFileList) {

                                    }
                                });
                            }else{
                                showToast("请先选择照片");
                            }

                        }

                    }
                    break;
            }
        }
    }


}
